/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ies.domain.model.web.converter;

import com.ies.domain.model.Pais;
import com.ies.domain.model.repository.facades.PaisFacade;
import com.ies.domain.model.util.JpaPool;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author jackson
 */
@FacesConverter(value="PaisConverter")
public class PaisConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        try {
            Long id = Long.parseLong(value.replaceAll("\\D", ""));
            PaisFacade paisFacade = (PaisFacade) JpaPool.getInstancia().getFacade(Pais.class);
            Pais pais = paisFacade.find(id);
            return pais;
        } catch (Exception ex) {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return String.valueOf(value);
    }
}
